package cn.lml.lession.auth.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import cn.lml.lession.auth.config.AuthProperties;

@Controller
public class LogoutController {

	@Autowired
	@Qualifier("consumerTokenServices")
	private ConsumerTokenServices consumerTokenServices;
	
	@Autowired
	private AuthProperties authProperties;

	@GetMapping("oauth/exit")
	public void exit(HttpServletRequest request, HttpServletResponse response, String access_token) throws IOException {
		if (consumerTokenServices.revokeToken(access_token)) {
			response.sendRedirect(authProperties.getAuthLoginUrl());
			new SecurityContextLogoutHandler().logout(request, null, null);
			//return Result.ok("注销成功");
		} else {
			//return Result.error("注销失败");
		}
	}

}